import { AppContext } from "../AppProvider"
import { useContext } from "react"

const usePermission = () => {
  const { setPermission, permission } = useContext(AppContext)
  const hasPermission = (code: string) => {
    if (permission && permission[code]) {
      return true
    }
    return false;
  }
  const updatePermission = (data: any[]) => {
    const map = {};
    const flatArray = (data: any[]) => {
      data.forEach(item => {
        map[item.path] = item.name;
        if (item.children) {
          flatArray(item.children);
        }
      })
    }
    flatArray(data);
    console.log('设置权限数据', map);
    setPermission({ ...map })
    // setAuth
  }
  return {
    hasPermission,
    updatePermission
  }
}
export default usePermission